﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для wRouteCash.xaml
    /// </summary>
    public partial class wRLCash : Window
    {
        private routelistEntities routelistEntities;
        public wRLCash()
        {
            InitializeComponent();
            routelistEntities = new routelistEntities();
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            SaveData();
        }

        private bool SaveData()
        {
            bool bResult = true;
            try
            {
                routelistEntities.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                bResult = false;
            }
            return bResult;
        }


        private bool IsModified()
        {
            bool bResult = false;
            if (null == routelistEntities) return false;
            bResult = routelistEntities.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added).Count() > 0 || routelistEntities.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Deleted).Count() > 0 || routelistEntities.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified).Count() > 0;
            return bResult;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (IsModified())
            {
                var result = MessageBox.Show("Сохранить изменения?", "Предупреждение", MessageBoxButton.YesNoCancel, MessageBoxImage.Exclamation, MessageBoxResult.Yes);
                switch (result)
                {
                    case MessageBoxResult.Yes:
                        if (false == SaveData())
                        {
                            e.Cancel = true;
                        }
                        break;
                    case MessageBoxResult.Cancel:
                        e.Cancel = true;
                        break;
                }

            }
        }

        //private System.Data.Objects.ObjectQuery<RouteListInfo> GetRouteListInfoQuery(routelistEntities routelistEntities)
        //{
        //    // Автоматически созданный код

        //    System.Data.Objects.ObjectQuery<RL.RouteListInfo> routeListInfoQuery = routelistEntities.RouteListInfo;
        //    // Для явной загрузки данных, возможно, потребуется добавить методы Include, аналогичные показанным ниже:
        //    // routeListInfoQuery = routeListInfoQuery.Include("RouteListInfo.Routes").
        //    // Дополнительные сведения см. по ссылке http://go.microsoft.com/fwlink/?LinkId=157380
        //    // Возвращает ObjectQuery.
        //    return routeListInfoQuery;
        //}

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            App app = (App)App.Current;
            datePicker1.SelectedDate = app.WorkDate;
            //RL.routelistEntities routelistEntities = new RL.routelistEntities();
            // Загрузить данные в RouteListInfo. Можно изменить этот код как требуется.
            System.Windows.Data.CollectionViewSource routeListInfoViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("routeListInfoViewSource")));
            //System.Data.Objects.ObjectQuery<RL.RouteListInfo> routeListInfoQuery = this.GetRouteListInfoQuery(routelistEntities);
            //routeListInfoViewSource.Source = routeListInfoQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);
            SetData(routeListInfoViewSource);
        }

        private void SetData(CollectionViewSource routeListInfoViewSource)
        {
            if (datePicker1.SelectedDate.HasValue == false) 
            {
                routeListInfoViewSource.Source = null;
                return;
            }
            DateTime beg_date = datePicker1.SelectedDate.Value.Date;
            DateTime end_date = beg_date.AddDays(1);
            routeListInfoViewSource.Source = from o in routelistEntities.RouteListInfo where o.RouteList.dt_datedoc >= beg_date && o.RouteList.dt_datedoc < end_date orderby o.RouteList.nomdog, o.Routes.number select o;
        }

        private void datePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource routeListInfoViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("routeListInfoViewSource")));
            SetData(routeListInfoViewSource);
        }
    }
}
